<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <TodoListHeader @subData="subData"></TodoListHeader>

    <!-- 主体部分 -->

    <TodoListBody
      :list="tabList"
      @del="del"
      @changeFlag="changeFlag"
      @checkALL='checkALL'
    ></TodoListBody>
    <!-- 底部部分 -->
    <TodoListFooter
      :sum="sum"
      @clearAll="clearAll"
      :btnShow="btnShow"
      :form="form"
      @changeForm="changeForm"
    ></TodoListFooter>
  </section>
</template>

<script>
import TodoListHeader from './components/TodoListHeader.vue'
import TodoListBody from './components/TodoListBody.vue'
import TodoListFooter from './components/TodoListFooter.vue'
export default {
  components: {
    TodoListHeader,
    TodoListBody,
    TodoListFooter,
  },
  data() {
    return {
      list: [
        { id: 1, content: '读万卷书', flag: false },
        { id: 2, content: '行万里路', flag: true },
      ],
      form: 'all',
    }
  },
  methods: {
    subData(value) {
      this.list = [
        ...this.list,
        { id: +new Date(), content: value, flag: false },
      ]
    },
    del(id, flag) {
      if (!flag) return alert('没完成不许删除')

      this.list = this.list.filter((item) => item.id !== id)
    },
    changeFlag(id) {
      this.list.forEach((item) => {
        if (item.id === id) {
          item.flag = !item.flag
        }
      })
    },
    clearAll() {
      this.list = this.list.filter((item) => !item.flag)
    },
    changeForm(value) {
      this.form = value
    },
    checkALL(){
      this.list=this.list.filter(item=> item.flag=true)
    }
  },
  computed: {
    sum() {
      return this.list.filter((item) => !item.flag).length
    },
    btnShow() {
      return this.list.some((item) => item.flag)
    },
    tabList() {
      switch (this.form) {
        case 'all':
          return this.list
        case 'active':
          return this.list.filter((item) => !item.flag)
        case 'completed':
          return this.list.filter((item) => item.flag)
        default:
          return this.list
      }
    },
  },
}
</script>

<style></style>
